<?php
namespace app\service;

class ImageGenerateService
{
    public function generateImage($promptText, $wallType, $quality)
    {

        // return [
        //     'generated_image' => 'https://lf-bot-studio-plugin-resource.coze.cn/obj/bot-studio-platform-plugin-tos/artist/image/66af9deceac74c3f84a88db932c2f1ec.png',
        //     'prompt' => '',
        //     'ai_processing_time' => 20
        // ];
        $startTime = microtime(true);

        $python_path = "python";
        if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
            $python_path = "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\python.exe";
        }
        $cmd = $python_path . " generate.py "  . escapeshellarg($wallType) . " " . escapeshellarg($quality) . " " . escapeshellarg($promptText) ;
        // debug_dump($cmd);
        $output = shell_exec($cmd);
        if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
            // Windows系统需要进行编码转换
            $output = iconv('GBK', 'UTF-8', $output);
        }
        // debug_dump($output,$cmd);
       
        $result = json_decode($output, true);
        if (!isset($result['data']) || empty($result['data'])){
            throw new \Exception($output);
        }
        
        $generatedImagePath = $result['data'];
        $endTime = microtime(true);
        $ai_processing_time = intval($endTime - $startTime);
        return [
            'generated_image' => $generatedImagePath,
            // 'prompt' => $prompt,
            'ai_processing_time' => $ai_processing_time
        ];
        


    }
}

